const router = require('koa-router')()
const path = require('path')
const fs = require('fs')

const multer = require('koa-multer');//加载koa-multer模块  
//文件上传  
//配置  
var storage = multer.diskStorage({
  //文件保存路径  
  destination: function (req, file, cb) {
    cb(null, 'public/file/')
  },
  //修改文件名称  
  filename: function (req, file, cb) {
    var fileFormat = (file.originalname).split(".");
    cb(null, Date.now() + "." + fileFormat[fileFormat.length - 1]);
  }
})
//加载配置  
var upload = multer({ storage: storage });
//路由  
router.post('/', upload.single('file'), async (ctx, next) => {
  ctx.body = {
    filename: ctx.req.file.filename, //返回文件名
    // path: path.join(__dirname, '../', ctx.req.file.path)
    path: ctx.req.file.path
  }
})

module.exports = router